import {reactive} from 'vue'
import {defineStore} from 'pinia'
import {stringify, parse} from 'zipson'

const MEMBER = "MEMBER"
export const useUserStore = defineStore('user', () => {
    const userInfo = reactive({
        id: '',
        mobile: '',
        token: ''
    })

    function setUserInfo({id, mobile, token}) {
        userInfo.id = id
        userInfo.mobile = mobile
        userInfo.token = token
    }

    function clearUserInfo() {
        userInfo.id = ''
        userInfo.mobile = ''
        userInfo.token = ''
    }

    return {userInfo, setUserInfo, clearUserInfo}
}, {
    persist: {
        key: MEMBER,
        storage: sessionStorage,
        // paths: ['count'],
        serializer: {
            deserialize: parse,
            serialize: stringify
        },
        beforeRestore: (ctx) => {
            console.log(`about to restore '${ctx.store.$id}'`)
        },
        afterRestore: (ctx) => {
            console.log(`just restored '${ctx.store.$id}'`)
        },
        debug: true,
    }
})